import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/login.vue'
import About from '../views/About.vue'
import Cart from '../views/Cart.vue'
import Person from '../views/Person.vue'
import Register from '../views/Register.vue'
import findcar from '../views/findcar.vue'
import State from '../views/State.vue'
import Myinfo from '../views/Myinfo.vue'
import Orderinfo from '../views/Orderinfo.vue'
import Psinfo from '../views/Psinfo.vue'
import Order from '../views/Order.vue'
import Rentout from '../views/RentOut.vue'
import Out from '../views/Out.vue'
import Comment from '../views/Comment.vue'


Vue.use(VueRouter)

const routes = [{
        path: '/home',
        name: 'Home',
        component: Home
    },
    {
        path: '/login',
        name: 'Login',
        component: Login

    },
    {
        path: '/about',
        name: 'info',
        component: About
    },
    {
        path: '/comment',
        name: 'Comment',
        component: Comment
    },
    {
        path: '/order',
        name: 'Order',
        component: Order
    },
    {
        path: '/RentOut',
        name: 'Rentout',
        component: Rentout
    },
    {
        path: '/state',
        name: 'State',
        component: State
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },
    {
        path: '/person',
        name: 'Person',
        component: Person,
        children: [
            { path: '/personalCenter/myinfo', component: Myinfo },
            { path: '/personalCenter/orderinfo', component: Orderinfo },
            { path: '/personalCenter/psinfo', component: Psinfo },
        ]
    },
    {
        path: '/findcar',
        name: 'findcar',
        component: findcar
    },
    {
        path: '/out',
        name: 'Out',
        component: Out
    },

    {
        path: '/cart',
        name: 'Cart',
        component: Cart
    }

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach(
    (to, from, next) => {
        if (to.path == "/login" || to.path == "/home" || to.path == "/register") {
            next();
        } else {
            if (localStorage.getItem('loginUser') != null) {
                next();
            } else {
                if (from.path != '/login') {

                    router.push('/login')
                }
            }
        }
    }
)
export default router